"""
Case Type   : Postgis
Case Name   : 拷贝postgis相关动态库和配置文件适配功能
Create At   : 2024/12/23
Owner       : haomeng
Description :
    1、拷贝postgis相关动态库和配置文件至对应路径下
    2、连接数据库，创建扩展
Expect      :
    1、相应文件拷贝成功
    2、连接数据库成功，创建扩展成功
History     :
    modified by haomeng:优化编译产物ftp路径信息
    modified by peilinqian at 2024-7-24;优化postgis路径 9.3优化脚本，使门禁重新执该用例
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class PostgisTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} 开始执行-----')
        self.common = Common()
        self.sh_primysh = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.root_node = Node('PrimaryRoot')
        self.constant = Constant()
        self.file_path1 = os.path.join(macro.DB_INSTANCE_PATH, '../app/lib')
        self.file_path2 = os.path.join(macro.DB_INSTANCE_PATH,
                                       '../app/share/postgresql/extension')
        self.file_path3 = os.path.join(self.file_path1, 'postgresql')
        self.postgis_path = os.path.join(macro.FTP_PLUGINS_PATH, 'postgis')
        self.postgis_x86 = os.path.join(self.postgis_path, 'plat_X86')
        self.postgis_arm = os.path.join(self.postgis_path, 'plat_ARM')

        platform = ['x86_64', 'aarch64']
        cmd = 'arch'
        res = self.user_node.sh(cmd).result()
        self.src_file_path = self.postgis_x86 if res.strip() == platform[
            0] else self.postgis_arm
        self.log.info(self.src_file_path)

    def test_postgis(self):
        file_list = ['libjson-c.so.2', 'libgeos_c.so.1', 'libproj.so.9',
                     'libgeos-3.6.2.so', 'libgdal.so.1.18.0',
                     'liblwgeom-2.4.so.0', 'postgis.control',
                     'postgis--2.4.2.sql', 'postgis_raster--2.4.2.sql',
                     'postgis_raster.control', 'postgis_topology--2.4.2.sql',
                     'postgis_topology.control', 'postgis-2.4.so',
                     'rtpostgis-2.4.so', 'postgis_topology-2.4.so']

        text = '---step1:拷贝相关动态库和配置文件到数据库对应路径下   expect:成功---'
        self.log.info(text)

        for file in file_list[0:6]:
            text = f'---copy {file}文件---'
            self.log.info(text)
            copy_cmd1 = f'wget -N -P {self.file_path1} ' \
                f'{self.src_file_path}/{file};'
            self.log.info(copy_cmd1)
            copy_res1 = self.root_node.sh(copy_cmd1).result()
            self.log.info(copy_res1)
            cat_cmd1 = f'ls {self.file_path1} | grep {file}'
            self.log.info(cat_cmd1)
            cat_res1 = self.user_node.sh(cat_cmd1).result()
            self.log.info(cat_res1)
            self.assertIn(file, cat_res1, '执行失败' + text)

        text = '---重命名libgdal.so.1.18.0---'
        self.log.info(text)
        mv_cmd = f"mv {os.path.join(self.file_path1, 'libgdal.so.1.18.0')} " \
            f"{os.path.join(self.file_path1, 'libgdal.so.1')}"
        self.log.info(mv_cmd)
        mv_res = self.root_node.sh(mv_cmd).result()
        self.log.info(mv_res)

        for file in file_list[6:12]:
            text = f'---copy {file}文件---'
            self.log.info(text)
            copy_cmd2 = f'wget -N -P {self.file_path2} ' \
                f'{self.src_file_path}/{file};'
            self.log.info(copy_cmd2)
            copy_res2 = self.root_node.sh(copy_cmd2).result()
            self.log.info(copy_res2)
            cat_cmd2 = f'ls {self.file_path2} | grep {file}'
            self.log.info(cat_cmd2)
            cat_res2 = self.root_node.sh(cat_cmd2).result()
            self.log.info(cat_res2)
            self.assertIn(file, cat_res2, '执行失败' + text)

        for file in file_list[12:15]:
            text = f'---copy {file}文件---'
            self.log.info(text)
            copy_cmd3 = f'wget -N -P {self.file_path3} ' \
                f'{self.src_file_path}/{file};'
            self.log.info(copy_cmd3)
            copy_res3 = self.root_node.sh(copy_cmd3).result()
            self.log.info(copy_res3)
            cat_cmd3 = f'ls {self.file_path3} | grep {file}'
            self.log.info(cat_cmd3)
            cat_res3 = self.root_node.sh(cat_cmd3).result()
            self.log.info(cat_res3)
            self.assertIn(file, cat_res3, '执行失败' + text)

        chmod_cmd = f'chown -R {self.user_node.ssh_user}:' \
            f'{self.user_node.ssh_user} {self.file_path1};' \
            f'chown -R {self.user_node.ssh_user}:{self.user_node.ssh_user} ' \
            f'{self.file_path2};' \
            f'chown -R {self.user_node.ssh_user}:{self.user_node.ssh_user} ' \
            f'{self.file_path3};'
        self.log.info(chmod_cmd)
        chmod_res = self.root_node.sh(chmod_cmd).result()
        self.log.info(chmod_res)

        text = '---step2:创建extension; expect:成功---'
        self.log.info(text)
        sql_cmd = 'drop extension if exists postgis cascade;' \
                  'create extension postgis;'
        self.log.info(sql_cmd)

        sql_res = self.sh_primysh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE EXTENSION', sql_res)

    def tearDown(self):
        self.log.info('------No need to clean------')
        self.log.info(f'-----{os.path.basename(__file__)} 执行结束-----')
